// import { defineConfig } from "@mikro-orm/mariadb";
// import { ReflectMetadataProvider, defineConfig } from "@mikro-orm/mysql";
import { ReflectMetadataProvider, defineConfig } from "@mikro-orm/mariadb";
import { EntityGenerator } from "@mikro-orm/entity-generator";
import dotenv from "dotenv-flow";
import { Book } from "./entities/Book";
import { Author } from "./entities/Author";
import { App } from "./entities/App";
import { User } from "./entities/User";
import { AppUser } from "./entities/AppUser";
import { Config } from "./entities/Config";
import { Dataset } from "./entities/Dataset";
import { Endpoint } from "./entities/Endpoint";

import { TsMorphMetadataProvider } from "@mikro-orm/reflection";
import { loadSecrets } from "./utils/secret";

dotenv.config({
  path: "./env",
});

let { MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT } =
  process.env;

console.log("🚀 ~ process.env.NODE_ENV:", process.env.NODE_ENV);
console.log("🚀 ~ process.env.DB_ENV:", process.env.DB_ENV);
// if (["production", "development"].includes(process.env.NODE_ENV)) {
if (process.env.DB_ENV !== "local") {
  const secretData = loadSecrets();
  MYSQL_HOST = secretData.MYSQL.HOST;
  MYSQL_USER = secretData.MYSQL.USERNAME;
  MYSQL_PASSWORD = secretData.MYSQL.PASSWORD;
  MYSQL_DATABASE = secretData.MYSQL.DATABASE;
  MYSQL_PORT = secretData.MYSQL.PORT;
}
// console.log("🚀 ~ =========== process.env:", process.env);
// console.log("🚀 ~ ========wwww=== process.cwd():", process.cwd());

export default defineConfig({
  // It is important that entities will point to the compiled JS files, and entitiesTs will point to the TS source files. You should not mix those.

  // !!!! 注意 在nextjs中使用 micro-orm, entities的指定需要是 类对象，
  // !!! 不能指定路径，否则会提示  SyntaxError: Cannot use import statement outside a module
  // entities: [Book, Author, App, User],
  // entitiesTs: [Book, Author, App, User],

  // entities: [".next/server/chunks/*.js"],
  // entitiesTs: [Book, Author, App, User],

  // entities: [".next/server/chunks/*.js"],
  entities: [Book, Author, App, User, AppUser, Config, Dataset, Endpoint],

  // default since v4, so not needed to specify explicitly
  // metadataProvider: ReflectMetadataProvider,
  // metadataProvider: TsMorphMetadataProvider,
  host: MYSQL_HOST,
  port: Number(MYSQL_PORT),
  user: MYSQL_USER || "root",
  password: MYSQL_PASSWORD || "root",
  dbName: MYSQL_DATABASE,

  // type: 'mariadb',

  allowGlobalContext: true,

  // extensions: [EntityGenerator],

  debug: process.env.NODE_ENV !== "production",
});
